package com.tg.demo.hutool;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author Rubble
 * Created on 2020/5/18
 */
public class DemoHutoolApp {
    public static void main(String[] args) {


//        idUtilTest();
//        int val = 135;
//        int workId = val & 31;
//        int dataCenterId = val>>5;
//        Console.log("workerId {} dataCenterId {}",workId,dataCenterId);
        // 异或 5
//        System.out.println(6^3);
        System.out.println(RandomUtil.randomInt(1000,9999));

    }

    public static void convertTest() {
        //1 convert
        String[] arr = new String[]{"a","b","c"};
        List<String> convertList = (List<String>) Convert.toList(arr);
        System.out.println("convertList  "+JSON.toJSONString(convertList));

        String a = "2017-05-06";
        String b = "2017-05-06 20:01:01";
        Date conertData = Convert.toDate(a);
        Date conertDatab = Convert.toDate(b);

        System.out.println("convert data "+conertData);
        System.out.println("convert data "+conertDatab);

    }
    public static void dateTest(){

        String dateStr = "2017-03-01 22:33:23";
        Date date = DateUtil.parse(dateStr);

        //一天的开始，结果：2017-03-01 00:00:00
        Date beginOfDay = DateUtil.beginOfDay(date);

        System.out.println(DatePattern.NORM_DATE_PATTERN);


    }

    public static void ioTest(){

    }

    public static void idUtilTest(){

        //根据long值获取ip v4地址
        String ipAddr= NetUtil.getLocalhost().getHostAddress();


//根据ip地址计算出long型的数据
        long ip= NetUtil.ipv4ToLong(ipAddr);

        System.out.println("============== "+ip);

        Snowflake snowflake = IdUtil.createSnowflake(1L, ip);

        long id = snowflake.nextId();

        System.out.println("============== "+id);

    }


}
